class Solution:
    def longestConsecutive(self, nums) -> int:
        if len(nums) == 0:
            return 0
        max_len = 1
        nodeList = []
        for num in nums:
            added = False
            for i in range(len(nodeList)):

                if nodeList[i].start == num + 1:
                    added = True
                    nodeList[i].start = num
                    if max_len < nodeList[i].end - nodeList[i].start:
                        max_len = nodeList[i].end - nodeList[i].start
                    break

                if nodeList[i].start > num:
                    added = True
                    nodeList.insert(i, Node(num, num + 1))
                    break

                if nodeList[i].start <= num:
                    if num < nodeList[i].end:
                        added = True
                        break
                    elif num == nodeList[i].end:
                        added = True
                        nodeList[i].end = nodeList[i].end + 1
                        if i < len(nodeList)-1 and nodeList[i].end == nodeList[i + 1].start:
                            nodeList[i].end = nodeList[i + 1].end
                            nodeList.pop(i+1)
                        if max_len < nodeList[i].end - nodeList[i].start:
                            max_len = nodeList[i].end - nodeList[i].start
                        break
            if not added:
                nodeList.append(Node(num, num + 1))

        return max_len


class Node:
    def __init__(self, start, end):
        self.start = start
        self.end = end


if __name__ == '__main__':
    so = Solution()
    t = [1,2,0,1]
    r = so.longestConsecutive(t)
    print(r)
